<template>
  <link type="text/css" rel="stylesheet" href="/assets/css/style.css">
  <link rel="stylesheet" href="/assets/css/coupon-info.css">
  <div class="coupon_cover">
      <van-image width="100%" height="200" :src="coupon.thumb_text" />
  </div>

  <div class="coupon_detail">
      <div class="coupon_info">
          <div class="left">
              <div class="left_top">
                  <div>
                      <span>{{coupon.rate*100}}</span>折
                  </div>
                  <div class="top_info">
                      <div>{{coupon.title}}</div>
                      <div>COUPON</div>
                  </div>
              </div>
              <div class="left_tips">订单满<span>100</span>打<span>{{coupon.rate*100}}</span>折</div>
          </div>
          <div class="receive">
              <div v-if="coupon.receive">
                <button class="right_btn" @click="receive">立即领取</button>
              </div>
              <div v-else>
                <button class="receive_btn">已领取</button>
              </div>
          </div>
      </div>
      <div class="coupon_prompt">
          <div class="prompt_title">温馨提示：</div>
          <div><span>•</span>活动开始时间：{{coupon.createtime_text}}</div>
          <div><span>•</span>活动结束时间：{{coupon.endtime_text}}</div>
          <div><span>•</span>剩余数量：{{coupon.total}}张，每人限领1张。快来领取！</div>
      </div>
  </div>

</template>

<script setup>
  const {proxy} = getCurrentInstance()

  let cid = proxy.$route.query.cid ? proxy.$route.query.cid : 0

  let coupon = ref({})

  onBeforeMount(() => {
    info()
  })

  //获取优惠券详细
  const info = async () => 
  {
      var params = { cid }

      //判断是否有没有登录
      if (proxy.$business.value.id) {
        params.busid = proxy.$business.value.id
      }
      
      var result = await proxy.$POST({
        url: '/coupon/info',
        params
      })

      if(result.code == 0)
      {
        proxy.$fail(result.msg);
        return false
      }

      coupon.value = result.data.info
  }

  //领取
  const receive = async () =>
  {
      var params = { cid }

        //判断是否有没有登录
        if (proxy.$business.value.id) {
          params.busid = proxy.$business.value.id
        }
        
        var result = await proxy.$POST({
          url: '/coupon/receive',
          params
        })

        if(result.code == 0)
        {
          proxy.$fail(result.msg);
          return false
        }else
        {
          proxy.$success({
                message: result.msg,
                onClose: () =>
                {
                    proxy.$router.go(-1)
                }
            })
        }
  }

</script>